package cn.itsource.org.controller;

import cn.itsource.base.util.AjaxResult;
import cn.itsource.base.util.PageBean;
import cn.itsource.org.domain.Department;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.query.EmployeeQuery;
import cn.itsource.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//常用注解：
//	类上:@Api(tags = "用户接口",description = "用户接口详细描述")
//	方法上:@ApiOperation(value = "用户查找",notes = "根据ID查找用户")
@RestController
@RequestMapping("/employee")
@Api(tags = "用户接口",description = "用户接口详细描述")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

//    /**
//     * 新增或修改
//     * @param emp
//     * @return
//     */
    @PutMapping
    @ApiOperation(value = "用户新增或修改",notes = "新增或修改")
    public AjaxResult saveOrUpdate(@RequestBody Employee emp){
        String operate  =null;
        try {
            if (emp.getId()==null) {
                operate = "新增";
                employeeService.save(emp);
            }else{
                operate = "修改";
                employeeService.update(emp);
            }
            return AjaxResult.createSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.createFailure(operate);
        }
    }

////    void remove(Long id);
    @DeleteMapping("/{id}")
    @ApiOperation(value = "用户删除",notes = "根据id删除")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            employeeService.remove(id);
            return AjaxResult.createSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.createFailure("删除");
        }
    }
    @PatchMapping("/delete")
    @ApiOperation(value = "用户删除",notes = "批量删除")
    public AjaxResult patch(@RequestBody List<Long> ids){
        try {
            employeeService.patch(ids);
            return AjaxResult.createSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.createFailure("删除");
        }
    }
    @GetMapping("/{id}")
    @ApiOperation(value = "用户查找",notes = "根据id查找某个用户")
    public Employee findById(@PathVariable("id") Long id){
        return employeeService.findById(id);
    }
    @GetMapping
    @ApiOperation(value = "用户查找",notes = "查找所有用户")
    public List<Employee> findAll(){
        return employeeService.findAll();
    }
    @GetMapping("/deptsOfEmps")
    @ApiOperation(value = "用户查找",notes = "查找用户的所有部门")
    public List<Department> getDeptsOfEmps(){
        return employeeService.getDeptsOfEmps();
    }
    @GetMapping("/shopsOfEmps")
    @ApiOperation(value = "用户查找",notes = "查找用户的所有店铺")
    public List<Shop> shopsOfEmps(){
        return employeeService.getShopsOfEmps();
    }
    @PostMapping
    @ApiOperation(value = "分页查询",notes = "分页查询")
    public PageBean<Employee> queryData(@RequestBody EmployeeQuery query){
//        System.err.println(query.getName());
        return employeeService.queryData(query);
    }

//
//
//    Employee findById(Long id );
//
//    /**
//     * 分页查询
//     *
//     */
//    //查询总条数
//    Integer queryCount(EmployeeQuery query);
//    //查询当前的页数
//    PageBean<Employee> queryData(EmployeeQuery query);
}
